package com.lhh.adminlte.controller;

import com.lhh.adminlte.entity.User;
import com.lhh.adminlte.model.JsonResponse;
import com.lhh.adminlte.model.PageResponse;
import com.lhh.adminlte.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Auther: hanhui.liu
 * @Date: 2018/4/26 09:24
 * @Description: 用户接口
 */
@Controller
@RequestMapping(value = "user", produces = "application/json; charset=UTF-8")
public class UserController extends BaseController {

    private static final String PATH = "/user";

    @Autowired
    UserService userService;

    @RequestMapping("index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView(PATH + "/user");
        List<Map<String, String>> options = new ArrayList<Map<String, String>>();
        Map<String, String> option1 = new HashMap<>();
        option1.put("name", "启用");
        option1.put("value", "1");
        options.add(option1);
        Map<String, String> option2 = new HashMap<>();
        option2.put("name", "禁用");
        option2.put("value", "2");
        options.add(option2);
        modelAndView.addObject("options", options);
        modelAndView.setViewName(PATH + "/index");
        return modelAndView;
    }

    @RequestMapping("list")
    @ResponseBody
    public String list(@RequestParam("nickName") String nickName, @RequestParam("loginName") String loginName,
                       @RequestParam("status") Integer status, @RequestParam("displayStart") Integer displayStart,
                       @RequestParam("displayLength") Integer displayLength) {

        List<User> users = userService.selectUsers(nickName, loginName, status, displayStart, displayLength);
        long count = userService.countUser(nickName, loginName, status);
        PageResponse pageResponse = new PageResponse(count, users);
        return new JsonResponse(200, "success", pageResponse).toString();
    }

    @RequestMapping("add")
    @ResponseBody
    public String add() {
        return "";
    }

//    public Object
}
